# -*- coding: utf-8 -*-..,
##############################################################################
#
#    @author: PhongND
#    Copyright (C) 2014 Crea8s (http://www.crea8s.com) All Rights Reserved.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp import tools
# ===== =====
# import pytz
# import tzlocal
# from tzlocal import get_localzone # $ pip install tzlocal
# ===== =====
# test 2: XMLRPC
import traceback
import sys
from xmlrpclib import ServerProxy, Error


def magento_connect(self, cr, uid, ids, context):
    print 'magento_connect.....'
    objInstance = self.pool.get('crea8s.magento.instance').search(cr, uid, [])
    
    try:
        instance = objInstance.browse(cr, uid, ids[0], context)
        server_address = instance.url
        if not server_address[-1:] == '/':
            server_address = server_address + "/"
        server_address = server_address + "index.php/api/xmlrpc/?wsdl"
        server = ServerProxy(server_address)
        session = server.login(instance.api_user, instance.api_key)
        print '+ success!'
        return [True, server, session]
    except:
        traceback.print_exc()
        print '+ fail!'
        return [False, sys.exc_info()[0], False]
    
    return True
    
#     try:
#         ids = self.pool.get('setting.magento').search(cr, uid, [])
#         export = self.pool.get('setting.magento').browse(cr, uid, [ids[0]])
#         server_address = export[0]['url']
#         if not server_address[-1:] == '/':
#             server_address = server_address + "/"
#         server_address = server_address + "index.php/api/xmlrpc/?wsdl"
#         server = ServerProxy(server_address)
#         session = server.login(export[0]['api_user'], export[0]['api_pwd'])
#         return [True, server, session]
#     except:
#         traceback.print_exc()
#         return [False, sys.exc_info()[0], False]
            
            
            